package students_files_test01;

public class XMLSalesFormatter implements SalesFormatter{

    static public XMLSalesFormatter singletonInstance = new XMLSalesFormatter();

    private XMLSalesFormatter(){

    }

    static public XMLSalesFormatter getSingletonInstance(){
        return singletonInstance;
    }

    @Override
    public String formatSales(Sales sales) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" <Sales>\n");
        for (Order order : sales){
            double d = 0;
            for (OrderItem orderItem : order){
                d += orderItem.getValue();
            }
            stringBuilder.append("      <Order total=" + "\"" + d + "\"" + ">\n");
            for (OrderItem orderItem : order){
                stringBuilder.append("     <OrderItem quantity=\"" + orderItem.getQuantity() + "\" "
                + "price=\"" + orderItem.getProduct().getPrice() + "\">" + orderItem.getProduct().getCode() + "</OrderItem>\n");
            }
            stringBuilder.append("  </Order>\n");
        }
        stringBuilder.append("</Sales>\n");
        String s = stringBuilder.toString();
        return s;
    }
}
